﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Ecobee.Net
{
	/// <summary>
	/// Contains a list of predefined URI's for the API.
	/// </summary>
	public class KnownUri
	{
		private Api _api = null;

		/// <summary>
		/// 
		/// </summary>
		/// <param name="api"></param>
		public KnownUri(Api api)
		{
			this.Api = api;
		}

		/// <summary>
		/// 
		/// </summary>
		protected Api Api
		{
			get
			{
				return _api;
			}
			set
			{
				_api = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="refreshToken"></param>
		/// <returns></returns>
		public string RefreshToken(Token refreshToken)
		{
			return string.Format("{0}/token?grant_type=refresh_token&code={1}", this.Api.BaseUri, refreshToken.Identifier);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="authorizationToken"></param>
		/// <returns></returns>
		public string RequestAccess(Token authorizationToken)
		{
			return string.Format("{0}/token?grant_type=ecobeePin&code={1}", this.Api.BaseUri, authorizationToken.Identifier);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="scope"></param>
		/// <returns></returns>
		public string GetPinAuthorization(AuthorizationScope scope)
		{
			return string.Format("{0}/authorize?response_type=ecobeePin&scope={1}", this.Api.BaseUri, CamelCaseStringEnumConverter.ToCamelCase(scope));
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="thermostatSummary"></param>
		/// <returns></returns>
		public string RequestThermostatSummary(ThermostatSummary thermostatSummary)
		{
			return string.Format("{0}/thermostatSummary?json={1}", this.Api.ApiUri, WebUtility.UrlEncode(thermostatSummary.Serialize()));
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="apiObject"></param>
		/// <returns></returns>
		public string Thermostat(ApiObject apiObject = null)
		{
			string returnValue = string.Empty;

			if (apiObject != null)
			{
				string ser = apiObject.Serialize();
				returnValue = string.Format("{0}/thermostat?json={1}", this.Api.ApiUri, WebUtility.UrlEncode(apiObject.Serialize()));
			}
			else
			{
				returnValue = string.Format("{0}/thermostat", this.Api.ApiUri);
			}

			return returnValue;
		}
	}
}
